package com.mo.lib.utils.res

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.TypedValue
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.dylanc.longan.application
import java.io.ByteArrayOutputStream
import java.io.IOException

/**
 * @ author：mo
 * @ data：2018/1/29：13:44
 * @ 功能：res工具类
 */
object ResUtil {
    private val mTmpValue = TypedValue()



    /** 获取颜色值*/
    fun Context.getColor(@ColorRes colorId: Int) = ContextCompat.getColor(this, colorId)
    fun getColor(colorStr: String) = Color.parseColor(colorStr)

    /**根据color文件的名字取得id */
    fun Context.getColorId(name: String) = resources.getIdentifier(name, "color", application.packageName)

    /** 得到string.xml中的字符串*/
    fun Context.getString(resId: Int) = resources.getString(resId)

    /**  得到string.xml中的字符串，带点位符*/
    fun Context.getString(id: Int, vararg formatArgs: Any?) = resources.getString(id, *formatArgs)

    /** 根据string的名字取得id*/
    fun Context.getStringId(name: String?): Int = resources.getIdentifier(name, "string", application.packageName)

    /** 得到string.xml中和字符串数组*/
    fun Context.getStringArr(resId: Int): Array<String> = resources.getStringArray(resId)

    /**根据id拿到Drawable */
    fun Context.getDrawable(resId: Int): Drawable? = ContextCompat.getDrawable(this, resId)

    /** 根据layout文件的名字取得id*/
    fun Context.getLayoutId(name: String): Int? = resources.getIdentifier(name, "layout", application.packageName)

    /**根据drawable文件的名字取得id */
    fun Context.getDrawableId(name: String): Int? = resources.getIdentifier(name, "drawable", application.packageName)

    /**根据style的名字取得id */
    fun Context.getStyleId(name: String): Int? = resources.getIdentifier(name, "style", application.packageName)

    /** 根据id的名字取得id*/
    fun Context.getId(name: String): Int = resources.getIdentifier(name, "id", application.packageName)

    /**根据array的名字取得id */
    fun Context.getArrayId(name: String): Int = resources.getIdentifier(name, "array", application.packageName)

    /** 获取Assets文件下的数据*/
    fun Context.getAssetsString(assName: String): String? {
        val outputStream = ByteArrayOutputStream()
        var str: String? = null
        try {
            val `is` = resources.assets.open(assName!!)
            val bytes = ByteArray(1024)
            var length = 0
            while (`is`.read(bytes).also { length = it } != -1) {
                outputStream.write(bytes, 0, length)
            }
            `is`.close()
            outputStream.close()
            str = outputStream.toString()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return str
    }

    /** 获取bitmap*/
    fun Context.getBitmap(@DrawableRes resId: Int): Bitmap {
        val drawable = ContextCompat.getDrawable(this, resId)
        val canvas = Canvas()
        val bitmap = Bitmap.createBitmap(drawable!!.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
        canvas.setBitmap(bitmap)
        drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
        drawable.draw(canvas)
        return bitmap
    }


}